#include "headfiles.h"

using namespace std;

int main(int argc, char **argv) {
    if (argc != 2) {
        cout << "Usage: " << argv[0] << " file" << endl;
        return 0;
    }

    vector<int> vec;
    fstream fs;
    fs.open(argv[1], iostream::in);
    if (!fs.is_open()) {
        cout << "File open error!" << endl;
        return 1;
    }

    string str;
    while (fs >> str) {
        if (str.empty()) {
            break;
        }

        int val = atoi(str.c_str());
        vec.push_back(val);
    }
    fs.close();

    if (vec.size() & 0x1) {
        cout << "Input is incorrect!" << endl;
        return 1;
    }

    int n = vec.size() / 2;
    int index = 0;
    int *pre_order = new int[n];
    int *in_order = new int[n];
    vector<int>::iterator it;
    for (it = vec.begin(); it != vec.end(); it++) {
        pre_order[index] = *it;
        it++;
        in_order[index] = *it;
        index++;
    }

    TNODE *root = preOrderInOrderBinaryTree(pre_order, in_order, n);
    delete pre_order;
    delete in_order;

    printBinaryTreeLevel(root);

    destroyBinaryTree(root);

    return 0;
}

